package parser;

import model.Graph;
import model.Noeud;
import test.Test;

import java.io.*;
import java.text.Normalizer;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Thomas
 * Date: 20/02/14
 * Time: 18:37
 */
public class ReadCSV {
    private static final int    IMAGE_WIDTH     = 1000;
    private static final int    IMAGE_HEIGHT    = 1000;
    private static final int    IMAGE_PADDING   = 50;

    public static void main(String[] args) {
        try {
            Graph graph = ParserGraph.parse(Test.getGraphFile().getGraphFilename());
            GraphFilePxPosition.read(graph);

            ReadCSV obj = new ReadCSV();
            obj.run(graph);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void run(Graph graph) throws FileNotFoundException, UnsupportedEncodingException {

        String csvFile = "./Station_Bus_Ratp/ratp_arret_graphique_01.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = "#";
        PrintWriter writer = new PrintWriter("./ressources/test.test", "UTF-8");
        ArrayList<Integer> ids = new ArrayList<Integer>();

        double lat_min = 100000000;
        double lat_max = 0;
        double long_min = 100000000;
        double long_max = 0;
        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] station = line.split(cvsSplitBy);

                String name = station[3].replace('-',' ').replace("METRO", "").replace("RER", "").replace("TRAMWAY", "").replace(" ", "").toUpperCase();
                double latitude = Double.parseDouble(station[2]);
                double longitude = Double.parseDouble(station[1]);
                System.out.println("latitude: "+latitude);
                System.out.println("longitude: "+longitude);

                lat_min = Math.min(lat_min,latitude);
                if (latitude < 100)
                    lat_max = Math.max(lat_max,latitude);
                long_min = Math.min(long_min,longitude);
                if (longitude < 100)
                    long_max = Math.max(long_max,longitude);

                int mapWidth = 991;
                int mapHeight = 768;

                double mapLonLeft = 2.07107449147842;
                double mapLonRight = 2.78308875356856;
                double mapLonDelta = mapLonRight - mapLonLeft;

                double mapLatBottom = 48.6697931159572;
                double mapLatBottomDegree = mapLatBottom * Math.PI / 180;
                double worldMapWidth = ((mapWidth / mapLonDelta) * 360) / (2 * Math.PI);
                double mapOffsetY = (worldMapWidth / 2 * Math.log((1 + Math.sin(mapLatBottomDegree)) / (1 - Math.sin(mapLatBottomDegree))));

                double x = (longitude - mapLonLeft) * (mapWidth / mapLonDelta);
                double y = 0.1;
                if (latitude < 0) {
                    latitude = latitude * Math.PI / 180;
                    y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(latitude)) / (1 - Math.sin(latitude)))) - mapOffsetY);
                } else if (latitude > 0) {
                    latitude = latitude * Math.PI / 180;
                    latitude = latitude * -1;
                    y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(latitude)) / (1 - Math.sin(latitude)))) - mapOffsetY);
                    y = mapHeight - y;
                } else {
                    y = mapHeight / 2;
                }

                y += 156000;

                x *= 20;
                y *= 20;

                String choose = "";

                for (Noeud noeud : graph.getNoeuds().values()) {
                    String noeudname = Normalizer.normalize(noeud.getName(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "").replaceAll("^a-zA-Z0-9\\s","").replace('-',' ').replace('É','E').replace(" ", "").replace(",", "").toUpperCase();
                    if (( noeudname.contains(name) || name.contains(noeudname)) && !ids.contains(noeud.getId())) {
                        writer.println(noeud.getId() + " " + x + " " + y);
                        ids.add(noeud.getId());
                        choose = noeud.getName() + " id: " + noeud.getId();
                        break;
                    }
                }

                System.out.println("Station [nom= " + name
                        + " , latitude=" + x
                        + " , longitude=" + y
                        + " , found=" + choose + "]");

            }
            writer.close();

            System.out.println("lat_min: "+lat_min);
            System.out.println("lat_max: "+lat_max);
            System.out.println("long_min: "+long_min);
            System.out.println("long_max: " + long_max);
            System.out.println(Normalizer.normalize("La défense", Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", ""));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("Done");
    }

}
